import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QWidget, QVBoxLayout, QHBoxLayout,
                             QPushButton, QLabel, QLineEdit, QFileDialog, QTabWidget, QTextEdit)
from ansys.fluent.visualization import set_config
from ansys.fluent.visualization.pyvista import Graphics


class DisplayTab(QWidget):
    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent
        self.initUI()

    def initUI(self):
        vbox = QVBoxLayout()

        # 结果展示按钮
        self.displayButton = QPushButton("显示风速平面图像", self)
        self.displayButton.clicked.connect(self.display_velocity_vector)
        vbox.addWidget(self.displayButton)

        self.cutPlaneButton = QPushButton("显示横切面风速云图", self)
        self.cutPlaneButton.clicked.connect(self.display_cut_plane)
        vbox.addWidget(self.cutPlaneButton)

        # 结果展示标签
        self.resultLabel = QLabel("结果展示", self)
        vbox.addWidget(self.resultLabel)

        self.setLayout(vbox)

    def display_velocity_vector(self):
        if not self.parent.session:
            self.parent.consoleOutput.append(
                '<span style="color:red;">Fluent 会话未启动。</span>')
            return

        set_config(blocking=True, set_view_on_display="isometric")
        self.parent.session.tui.file.read_case_data(
            'output.cas.h5')  # 读取计算结果文件

        graphics = Graphics(session=self.parent.session)
        mesh1 = graphics.Meshes["mesh-1"]
        mesh1.show_edges = True
        mesh1.surfaces_list = ['part4:1', 'inlet', 'outlet', 'wall']
        velocity_vector = graphics.Vectors["velocity-vector"]
        velocity_vector.surfaces_list = ["part4:1"]
        velocity_vector.scale = 1
        velocity_vector.display("wind-1")
        self.parent.consoleOutput.append(
            '<span style="color:green;">风速平面图像已显示。</span>')

    def display_cut_plane(self):
        if not self.parent.session:
            self.parent.consoleOutput.append(
                '<span style="color:red;">Fluent 会话未启动。</span>')
            return

        set_config(blocking=True, set_view_on_display="isometric")
        self.parent.session.tui.file.read_case_data(
            'output.cas.h5')  # 读取计算结果文件

        graphics = Graphics(session=self.parent.session)
        surf_xy_plane = graphics.Surfaces["xy-plane"]
        surf_xy_plane.definition.type = "plane-surface"
        plane_surface_xy = surf_xy_plane.definition.plane_surface
        plane_surface_xy.z = -3.839  # 设置任意坐标点的横切面
        mesh1 = graphics.Meshes["mesh-1"]
        mesh1.show_edges = True
        mesh1.surfaces_list = ['part4:1',
                               'inlet', 'outlet', 'wall', 'xy-plane']
        velocity_vector = graphics.Vectors["velocity-vector"]
        velocity_vector.surfaces_list = ["xy-plane"]
        velocity_vector.scale = 1
        velocity_vector.display("wind-1")
        self.parent.consoleOutput.append(
            '<span style="color:green;">横切面风速云图已显示。</span>')
